package com.mission.schedule.appwidget;

import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.RemoteViews;

import com.mission.schedule.R;

import java.util.HashSet;
import java.util.Set;

import static com.tencent.mm.sdk.platformtools.MMApplicationContext.getPackageName;

/**
 * Created by liwei on 2016/10/24.
 */

public class PreUtils {

    private static SharedPreferences sharedPreferences;

    public static void SetBoolean(Context context, String key, boolean value){
        sharedPreferences = context.getSharedPreferences("AppWidget", Context.MODE_PRIVATE);
        EdtorBooleanSP(key,value);
    }

    public static void SetString(Context context, String key, String value){
        sharedPreferences = context.getSharedPreferences("AppWidget", Context.MODE_PRIVATE);
        EdtorStringSP(key,value);
    }

    public static void SetInt(Context context, String key, int value){
        sharedPreferences = context.getSharedPreferences("AppWidget", Context.MODE_PRIVATE);
        EdtorIntSP(key,value);
    }

    public static void SetIds(Context context, String key, HashSet<String> value){
        sharedPreferences = context.getSharedPreferences("AppWidget", Context.MODE_PRIVATE);
        EdtorIntsSP(key,value);
    }

    public static Set<String> getIds(Context context, String key){
        sharedPreferences = context.getSharedPreferences("AppWidget", Context.MODE_PRIVATE);
        return sharedPreferences.getStringSet(key,new HashSet<String>());
    }

    public static int getInt(Context context, String key){
        sharedPreferences = context.getSharedPreferences("AppWidget", Context.MODE_PRIVATE);
        return sharedPreferences.getInt(key,-1);
    }
    public static Boolean getBoolean(Context context, String key){
        sharedPreferences = context.getSharedPreferences("AppWidget", Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean(key,false);
    }

    public static String getString(Context context, String key){
        sharedPreferences = context.getSharedPreferences("AppWidget", Context.MODE_PRIVATE);
        return sharedPreferences.getString(key,"");
    }



    private static void EdtorStringSP(String key, String value){
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key,value);
        editor.commit();
    }

    private static void EdtorBooleanSP(String key, boolean value){
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(key,value);
        editor.commit();
    }

    private static void EdtorIntSP(String key, int value){
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(key,value);
        editor.commit();
    }

    private static void EdtorIntsSP(String key, HashSet<String> value){
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putStringSet(key,value);
        editor.commit();
    }

    public static void RefreshAppWidget(Context context) {
//        AppWidgetManager manager = AppWidgetManager.getInstance(context);
//        ComponentName componentName = new ComponentName(context,WidgetA.class);
//        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.widget_b);
//        manager.updateAppWidget(componentName,remoteViews);
        Intent intentf = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        Bundle extras = new Bundle();
        intentf.putExtras(extras);
        context.sendBroadcast(intentf);
    }

}